Tags: #◇CSMODEL #◇Inference for Means #◇Computer Science #◇Data Science #◇Statistical Modeling #◇Python #◇Machine Learning
Module 08: Inference for Means
◇Key Concepts
1. Central Limit Theorem & The Student's t-Distribution
When estimating a population mean using a sample mean , the Central Limit Theorem (CLT) states that for independent observations from a population with mean and standard deviation , the sampling distribution of is approximately normal:
- The Unknown Problem: In almost all real-world applications, the true population standard deviation is unknown. We must estimate using the sample standard deviation , yielding an estimated standard error:
- Why the t-Distribution? Substituting for introduces additional variability into the test statistic, especially when sample size is small. Consequently, the standard normal distribution () underestimates tail probabilities. To account for this extra uncertainty, we use the Student's t-distribution.
- Properties of the t-Distribution:
Symmetric and bell-shaped, centered at 0.
Has thicker (heavier) tails than the standard normal distribution (), meaning extreme values are more likely under a t-distribution.
Parameterized by degrees of freedom ().
As increases (), the t-distribution approaches the standard normal distribution .
2. Conditions for Inference Using the t-Distribution
Before performing any t-test or constructing t-confidence intervals, two core conditions must be verified:
- Independence Condition:
Observations in the sample must be independent of one another.
Collected via random sampling or random assignment.
If sampling without replacement, the sample size must not exceed 10% of the population (). - Normality / Sample Size Condition:
If the population distribution is normal, the sampling distribution of is normal even for small ().
If the population distribution is not normal or unknown:
Small samples (): Data should be nearly normal with no severe skewness or prominent outliers.
Large samples (): The Central Limit Theorem guarantees that the sampling distribution of is nearly normal, even if the underlying population data exhibits moderate skewness.
3. One-Sample t-Test
- When to Use: Testing whether the sample mean from a single numeric variable significantly differs from a hypothesized population value .
- Hypotheses:
Null Hypothesis (): (No difference from hypothesized mean)
Alternative Hypothesis (): (Two-tailed) OR / (One-tailed) - Test Statistic:
- Degrees of Freedom: .
- Confidence Interval:
4. Paired Observations (Dependent Samples t-Test)
- When to Use: Comparing two sets of quantitative observations that are paired or matched (e.g., pre-test vs. post-test scores of the same individuals, textbook prices at UCLA Bookstore vs. Amazon for the exact same titles).
- Methodology: Paired data is reduced to a single vector of differences:
The inference is then conducted as a one-sample t-test on the differences . - Hypotheses:
(The true average difference is zero)
(The true average difference is non-zero) - Test Statistic:
- Degrees of Freedom: .
5. Independent Two-Sample t-Test (Unpaired Observations)
- When to Use: Comparing the means of two distinct, independent groups (e.g., birth weight of babies born to smoker vs. non-smoker mothers). Sample sizes and do not need to be equal.
- Hypotheses:
- Point Estimate: Difference between sample means .
- Standard Error (Welch's / Unequal Variances):
- Test Statistic:
- Degrees of Freedom ():
Conservative Manual Rule (Exam Standard): .
Welch-Satterthwaite Approximation: Used automatically byscipy.stats.ttest_ind(..., equal_var=False).
6. One-Tailed vs. Two-Tailed Tests & p-Value Adjustments
- Two-Tailed Test (): Measures extreme values in both tails. .
- One-Tailed Test ( or ): Measures extreme values in a specific direction.
- Converting p-values:
If test statistic aligns with the direction specified in :
If test statistic goes in the direction opposite of :
7. Comparing Multiple Means: ANOVA & Bonferroni Correction
- When to Use: Comparing means across independent groups simultaneously (e.g., batting averages among Outfielders, Infielders, and Catchers).
- Why Not Multiple Pairwise t-Tests? Running many pairwise t-tests causes Type I Error Inflation. If for a single test, running tests increases the overall probability of at least one false positive to .
- ANOVA Hypotheses:
(All group means are equal)
: At least one group mean is different. - ANOVA F-Statistic:
(Mean Square between Groups): Measures variability between group means ().
(Mean Square Error): Measures variability within groups (). - Post-Hoc Analysis & Bonferroni Correction:
If ANOVA rejects , conduct pairwise t-tests to identify specific group differences.
Apply Bonferroni Correction to control family-wise error rate:
8. Step-by-Step Manual Calculator Steps for t-Tests
- State Hypotheses: Define and clearly with population parameters ().
- Verify Assumptions: Check independence and normality / .
- Compute Sample Statistics: Calculate , , (or , or ).
- Calculate Standard Error ():
One-sample / Paired:
Independent Two-sample: - Compute Test Statistic ():
- Find Degrees of Freedom () & Critical Value ():
Look up in t-table for given and . - Make Decision & State Conclusion:
If (or -value ), reject .
If (or -value ), fail to reject .
State conclusion in plain language relative to the problem context.
◇Python Snippets
1. One-Sample t-Test (scipy.stats.ttest_1samp)
Tests if a sample vector data has a population mean equal to popmean.
import numpy as np
from scipy import stats
# Sample data: housing lot areas (in sq ft)
lot_area_sample = np.array([9500, 10200, 8900, 9800, 10500, 9100, 9400, 9900, 10100, 8800])
hypothesized_mean = 10000
# Perform one-sample t-test (two-sided by default)
res = stats.ttest_1samp(lot_area_sample, popmean=hypothesized_mean)
print(f"t-statistic: {res.statistic:.4f}")
print(f"p-value: {res.pvalue:.4f}")
print(f"Degrees of freedom: {res.df}")
# Decision rule at alpha = 0.05
alpha = 0.05
if res.pvalue < alpha:
print("Reject H0: Sample mean differs significantly from 10,000.")
else:
print("Fail to reject H0: Insufficient evidence of a difference.")
2. Independent Two-Sample t-Test (scipy.stats.ttest_ind)
Compares two independent samples. Use equal_var=False for Welch's t-test (standard best practice).
import pandas as pd
from scipy import stats
# Load birth records dataset
nc_df = pd.read_csv('nc.csv')
# Extract baby weights for smoker and non-smoker mothers
smoker_weights = nc_df[nc_df['habit'] == 'smoker']['weight'].dropna()
nonsmoker_weights = nc_df[nc_df['habit'] == 'nonsmoker']['weight'].dropna()
# Perform Welch's t-test (equal_var=False)
res_ind = stats.ttest_ind(smoker_weights, nonsmoker_weights, equal_var=False)
print(f"t-statistic: {res_ind.statistic:.4f}")
print(f"p-value: {res_ind.pvalue:.4f}")
# Interpretation:
# If p-value < 0.05, reject H0 (significant difference in newborn weight).
3. Paired Two-Sample t-Test (scipy.stats.ttest_rel)
Compares two dependent/paired samples (e.g., store price vs. Amazon price for the same items).
import numpy as np
from scipy import stats
# Prices of 6 textbooks at campus bookstore vs. Amazon
store_prices = np.array([30, 19, 19, 45, 25, 60])
amazon_prices = np.array([20, 15, 19, 38, 21, 52])
# Method A: Using scipy.stats.ttest_rel
res_paired = stats.ttest_rel(store_prices, amazon_prices)
# Method B: Equivalent one-sample t-test on differences
diffs = store_prices - amazon_prices
res_diff = stats.ttest_1samp(diffs, popmean=0)
print(f"Paired t-stat: {res_paired.statistic:.4f}, p-value: {res_paired.pvalue:.4f}")
print(f"Differences 1-sample t-stat: {res_diff.statistic:.4f}, p-value: {res_diff.pvalue:.4f}")
# Note: Both methods produce identical t-statistic and p-value!
4. Critical Values and Quantiles (scipy.stats.t.ppf)
Finds critical values for confidence intervals and hypothesis rejection regions.
from scipy import stats
n = 20
df = n - 1
alpha = 0.05
# Two-tailed critical t-value for 95% confidence level
t_star_95 = stats.t.ppf(1 - alpha / 2, df=df)
print(f"95% Two-tailed critical t-value (df={df}): {t_star_95:.4f}")
# One-tailed critical t-value for 95% confidence level (alpha = 0.05 in one tail)
t_star_one_tailed = stats.t.ppf(1 - alpha, df=df)
print(f"95% One-tailed critical t-value (df={df}): {t_star_one_tailed:.4f}")
5. Constructing Confidence Intervals for Means
Calculates margin of error and confidence bounds manually and using stats.t.interval.
import numpy as np
from scipy import stats
sample_data = np.array([4.4, 3.8, 5.1, 4.9, 4.2, 5.5, 3.9, 4.7, 4.1, 4.6])
n = len(sample_data)
sample_mean = np.mean(sample_data)
sample_std = np.std(sample_data, ddof=1) # Sample std (ddof=1)
se = sample_std / np.sqrt(n)
alpha = 0.05
df = n - 1
t_crit = stats.t.ppf(1 - alpha / 2, df=df)
margin_of_error = t_crit * se
ci_lower = sample_mean - margin_of_error
ci_upper = sample_mean + margin_of_error
print(f"Sample Mean: {sample_mean:.2f}")
print(f"Margin of Error: {margin_of_error:.4f}")
print(f"Manual 95% CI: ({ci_lower:.4f}, {ci_upper:.4f})")
# Built-in SciPy method:
ci_scipy = stats.t.interval(confidence=0.95, df=df, loc=sample_mean, scale=se)
print(f"SciPy 95% CI: ({ci_scipy[0]:.4f}, {ci_scipy[1]:.4f})")
6. One-Way ANOVA & Bonferroni Post-Hoc Correction
Tests equality of means across multiple groups and applies Bonferroni correction.
import numpy as np
from scipy import stats
outfielders = np.array([0.32, 0.31, 0.35, 0.29, 0.33, 0.30])
infielders = np.array([0.318, 0.310, 0.325, 0.305, 0.312, 0.320])
catchers = np.array([0.302, 0.295, 0.288, 0.310, 0.290, 0.300])
# 1. Run One-Way ANOVA
f_stat, p_val = stats.f_oneway(outfielders, infielders, catchers)
print(f"ANOVA F-statistic: {f_stat:.4f}, p-value: {p_val:.4f}")
# 2. Bonferroni Correction for post-hoc pairwise tests
k = 3 # Number of groups
num_comparisons = k * (k - 1) // 2 # 3 comparisons
alpha_original = 0.05
alpha_bonferroni = alpha_original / num_comparisons
print(f"Original Alpha: {alpha_original}")
print(f"Bonferroni Adjusted Alpha*: {alpha_bonferroni:.4f}")
# Compare Outfielders vs Catchers post-hoc
res_posthoc = stats.ttest_ind(outfielders, catchers, equal_var=False)
print(f"Outfielder vs Catcher p-value: {res_posthoc.pvalue:.4f}")
if res_posthoc.pvalue < alpha_bonferroni:
print("Significant difference between Outfielders and Catchers after Bonferroni correction.")
◇Formulas & Key Equations
| Concept / Test Type | Parameter / Quantity | Mathematical Formula | Degrees of Freedom () |
|---|---|---|---|
| Standard Error (1 Sample) | |||
| 1-Sample t-Statistic | |||
| 1-Sample Confidence Interval | |||
| Paired Samples t-Statistic | |||
| Independent Samples (Welch) | |||
| Independent Samples t-Statistic | |||
| ANOVA Between-Group Variance | |||
| ANOVA Within-Group Variance | |||
| ANOVA F-Statistic | |||
| Bonferroni Correction | N/A |
◇Common Exam Traps
-
Trap 1: Confusing Paired vs. Independent t-Tests
-
Mistake: Running
ttest_indon data where measurements come from the same individuals (e.g., pre-treatment vs. post-treatment weights). -
Correction: If observations are linked/paired, use
ttest_rel(or 1-sample t-test on differences). Using independent t-test ignores pairing and reduces test power. -
Trap 2: Forgetting
equal_var=Falsein SciPy -
Mistake: Calling
scipy.stats.ttest_ind(a, b)without specifyingequal_var=False. -
Correction: By default, SciPy sets
equal_var=True(Student's pooled t-test). In CSMODEL and modern statistics, Welch's t-test (equal_var=False) is required unless equal population variances are proven. -
Trap 3: Incorrect Degrees of Freedom Calculation
-
Paired t-test: (NOT ).
-
Independent t-test (Manual Exam Rule): (NOT unless pooled variance is assumed).
-
ANOVA: Numerator , Denominator .
-
Trap 4: One-Tailed p-Value Division Error
-
Mistake: Always dividing SciPy's
pvalueby 2 to get a one-tailed p-value. -
Correction: Dividing by 2 is valid ONLY IF the sample statistic lies in the direction of the alternative hypothesis. If the sample statistic goes against , the one-tailed p-value is . Alternatively, use
alternative='greater'oralternative='less'. -
Trap 5: Misinterpreting Normality Rule
-
Mistake: Believing that means the sample data itself becomes normally distributed.
-
Correction: The sample data distribution does not change. ensures that the sampling distribution of the mean is approximately normal due to the Central Limit Theorem.
-
Trap 6: Misinterpreting Confidence Intervals
-
Mistake: Claiming "There is a 95% probability that the true mean falls inside the interval ."
-
Correction: In frequentist statistics, is a fixed unknown constant, not a random variable. The correct phrasing is: "We are 95% confident that the true population mean lies between 3.29 and 5.51", meaning 95% of intervals constructed using this procedure will capture .
-
Trap 7: Omitting Bonferroni Correction for Multiple Comparisons
-
Mistake: Running multiple pairwise t-tests after ANOVA using standard .
-
Correction: Running tests inflates overall Type I error rate. You must compare p-values against .
◇Practice Questions
Q1. A factory manufactures precision steel rods. The target mean diameter is mm. A quality control inspector takes a random sample of rods and measures their diameters. The sample mean is mm and the sample standard deviation is mm.
Perform a two-tailed hypothesis test at to determine if the manufacturing process is out of calibration. (Given: For , ). A) ; Fail to reject because . B) ; Reject because . C) ; Fail to reject because . D) ; Reject because .
Q2. Inspect the following Python code snippet analyzing two independent website layout variations (A and B):
import numpy as np
from scipy import stats
layout_A = np.array([45, 52, 48, 60, 55, 50, 49, 53])
layout_B = np.array([38, 42, 40, 47, 44, 39, 41, 43])
stat, pval = stats.ttest_ind(layout_A, layout_B, equal_var=False)
print(f"Statistic: {stat:.3f}, p-value: {pval:.5f}")
Output:
Statistic: 3.842, p-value: 0.00179
What is the correct conclusion at a 5% significance level ()? A) Fail to reject because the t-statistic (3.842) is greater than 0.05. B) Reject using Student's paired t-test; layout A and B have identical performance. C) Reject using Welch's t-test (equal_var=False); there is a statistically significant difference in user engagement between layout A and layout B (). D) Accept ; layout B is significantly better than layout A.
Q3. A medical clinic tested a new cholesterol-lowering drug on patients. The blood cholesterol level (in mg/dL) was measured before and 8 weeks after treatment for each patient. The average difference (Before - After) was mg/dL, and the sample standard deviation of differences was mg/dL.
Construct the 95% confidence interval for the mean reduction in cholesterol. (Given: For , ). A) mg/dL B) mg/dL C) mg/dL D) mg/dL
Q4. Consider the Python script below testing whether a new compiler produces faster execution times than a benchmark standard of seconds:
from scipy import stats
exec_times = [23.5, 24.1, 22.8, 24.8, 23.9, 24.2, 23.0] # n = 7
res = stats.ttest_1samp(exec_times, popmean=25.0, alternative='less')
t_crit = stats.t.ppf(0.05, df=len(exec_times)-1)
print(f"t_stat = {res.statistic:.3f}, p_val = {res.pvalue:.4f}")
print(f"t_crit = {t_crit:.3f}")
Output:
t_stat = -4.382, p_val = 0.0023
t_crit = -1.943
Which statement correctly interprets this output at ? A) Fail to reject because execution times cannot be negative. B) Reject because the observed t-statistic () is more extreme than the critical value () and , proving the compiler significantly reduces execution time. C) Reject because . D) Fail to reject because a two-sided test was required instead of alternative='less'.
Q5. A data scientist compares the processing time of 3 different algorithm implementations (Algorithm A, B, and C) across multiple benchmark tests (, ).
An ANOVA test yields and .
Calculate the ANOVA -statistic and determine the Bonferroni-adjusted significance level for post-hoc pairwise comparisons at overall . A) , B) , C) , D) ,
Q6. Match each research scenario (1, 2, 3) to its appropriate statistical test function in Python SciPy:
- Comparing the exam scores of Section 1 vs. Section 2 (two separate groups of students).
- Testing if the average test score of Section 1 differs from the historical departmental mean of 75.0.
- Comparing midterm vs. final exam scores of the same group of students. A) 1:
ttest_rel, 2:ttest_1samp, 3:ttest_indB) 1:ttest_ind, 2:ttest_1samp, 3:ttest_relC) 1:ttest_1samp, 2:ttest_ind, 3:ttest_relD) 1:f_oneway, 2:ttest_ind, 3:ttest_1samp
Answer Key:
Q1: A — ; Fail to reject
Step-by-step solution:
- , .
- mm.
- Test Statistic .
- Compare to critical value ().
- Since , fail to reject . There is insufficient evidence to conclude the process is out of calibration.
Q2: C — Reject using Welch's t-test (equal_var=False)
Explanation: The code callsttest_ind(..., equal_var=False), which performs Welch's independent two-sample t-test. The resulting -value () is less than , so we reject and conclude that user engagement differs significantly between layouts.
Q3: A — mg/dL
Step-by-step solution: - , , , .
- mg/dL.
- Margin of Error mg/dL.
- mg/dL.
Q4: B — Reject because and
Explanation: The left-tailed test yields a negative t-statistic. Since is less than the critical value (falls deep into the rejection region) and , we reject .
Q5: A — ,
Step-by-step solution: - groups, .
- .
- .
- .
- .
- .
- .
- Pairwise comparisons .
- Bonferroni adjusted .
Q6: B — 1:ttest_ind, 2:ttest_1samp, 3:ttest_rel
Explanation: Scenario 1 compares two separate/independent groups (ttest_ind). Scenario 2 compares a single group against a known standard (ttest_1samp). Scenario 3 compares paired measurements from the same students (ttest_rel).
◇References & Navigation
- Previous Topic: ◇Foundations for Inference
- Next Topic: ◇Inference for Categorical Data
- Related Notes: ◇Foundations for Inference, ◇Bayesian Inference
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
When estimating a population mean with an unknown standard deviation, which distribution is used?
Which test is appropriate for comparing pre-test and post-test scores of the same individuals?
Why is Welchs t-test generally preferred over the standard Students independent t-test?
When conducting multiple pairwise t-tests, why is the Bonferroni correction used?
What is the correct degrees of freedom formula for a One-Sample t-Test?